package com.chinatechstar.component.commons.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author Rei
 * @date 2021/7/17
 */
public class DeserializerBigDecimal extends JsonDeserializer<BigDecimal> {
    /**
     * 出参保留两位小数
     *
     * @param jsonParser             jsonParser
     * @param deserializationContext deserializationContext
     * @return BigDecimal
     * @throws IOException IOException
     */
    @Override
    public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        if (Objects.isNull(jsonParser.getDecimalValue())) {
            return null;
        } else {
            return jsonParser.getDecimalValue().setScale(2, RoundingMode.HALF_UP);
        }
    }
}
